package com.tongji.showroom.view
{
	import com.tongji.showroom.events.GetSystemSceneEvent;
	import com.tongji.showroom.events.GetUserProductEvent;
	import com.tongji.showroom.events.HideFurnitureEvent;
	import com.tongji.showroom.events.ListItemMouseEvent;
	import com.tongji.showroom.events.ShowPictureEvent;
	import com.tongji.showroom.events.ShowRoomEvent;
	import com.tongji.showroom.model.FurnitureModel;
	import com.tongji.showroom.model.ProductModel;
	import com.tongji.showroom.model.SystemSceneModel;
	import com.tongji.showroom.model.UserModel;
	import com.tongji.showroom.utils.ConstVariableInventory;
	import com.tongji.showroom.utils.DragDropManager;
	import com.tongji.showroom.utils.PopUpAdmin;
	import com.tongji.showroom.utils.Showroom;
	import com.tongji.showroom.view.titleWindows.SingleFurnitureTitleWindow;
	import com.tongji.showroom.view.twoD.AddBitmap;
	import com.tongji.showroom.view.twoD.Furniture;
	
	import flash.display.BitmapData;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.utils.ByteArray;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.core.UIComponent;
	import mx.graphics.codec.JPEGEncoder;
	import mx.managers.DragManager;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	public class EditSceneMediator extends Mediator
	{
		[Inject]
		public var view:EditSceneView;
		
		[Inject]
		public var getSystemSceneModel:SystemSceneModel;
		
		[Inject]
		public var furnitureModel:FurnitureModel;
		
		[Inject]
		public var pictureModel:ProductModel;
		
		[Inject]
		public var productModel:ProductModel;
		
		[Inject]
		public var userModel:UserModel;
		
		public var flagUp1:int = 5;
		public var flagBelow1:int = 0;	
		public var flagUp:int = 3;	
		public var flagBelow:int = 0;
		private var _bitmap:BitmapData;
		private var wholeScene:Object = new Object(); //整个场景，Object类型，包含三个defaultScene
		public var selectedScene:Object = new Object(); //某个场景，Object类型，45度或者90度或者135度
		private var selectedAngle:int = 0; //所选中的角度，int类型
		private var singleFurnitureTitleWindow:SingleFurnitureTitleWindow = new SingleFurnitureTitleWindow();
		
		private var upBackGroundBitmap:AddBitmap;
		[Bindable]
		private var temp:ArrayCollection;
		[Bindable]
		private var furniturArray:ArrayCollection;
		
		override public function onRegister():void
		{
			super.onRegister();
			furniturArray = new ArrayCollection();
			BindingUtils.bindProperty(view.uploadPictureList,  "dataProvider", productModel, "tempArray");
			BindingUtils.bindProperty(view.briefMessageList, "dataProvider", getSystemSceneModel, "getSceneArr");
			/*if (getSystemSceneModel.getSceneArr.length > 0)
			{
				dispatch(new ShowRoomEvent(ShowRoomEvent.SHOW_ROOM, getSystemSceneModel.getSceneArr[0]));
				view.briefMessageList.selectedIndex = 0;
			}*/
/*			dispatch(new AddUserProductEvent(AddUserProductEvent.GET_USER_PRODUCT, userModel.userID));
*/			
			
		    eventMap.mapListener(view.ScreenShotBtn, MouseEvent.CLICK, screenShotHandler);
		    eventMap.mapListener(view, ListItemMouseEvent.ITEM_MOUSE_DOWN, itemMouseDownHandler, ListItemMouseEvent);
			addViewListener(ShowRoomEvent.SHOW_ROOM, showRoomHandler);
			this.addContextListener(ShowRoomEvent.SHOW_ROOM, showRoomHandler);
			
			view.briefMessageList.addEventListener(MouseEvent.CLICK, showSceneHandler);
		}
		
		private function showSceneHandler(event:MouseEvent):void
		{
			var temp:Object = view.briefMessageList.selectedItem;
			wholeScene = temp;
			selectedScene = temp.SceneAngleList[0];
			initScene(0);
			var showroom:Showroom = new Showroom(view.roomDisplay, temp, 0);		
		}

		/**
		 * @author caddy 
		 * @param event
		 * 弹出设置阴影的浮云框
		 */		
		private function popUpFilterWin(event:MouseEvent):void
		{
			/*			PopUpAdmin.popupCenterWindow("filterWin", view.parent, mediatorMap, false);
		*/		}
		private function smallScreenHandler(event:MouseEvent):void
		{
			var target:UIComponent = furnitureModel.vroom;
			var map:BitmapData = furnitureModel.vroom.content;
			var boundingBox:Rectangle = target.getRect(target);
			var matrix:Matrix = new Matrix();
			var bitmapData:BitmapData = new BitmapData(boundingBox.width, boundingBox.height);
			bitmapData.draw(map, matrix); 
			var jpg:JPEGEncoder = new JPEGEncoder(); 
			var jpgByteArray:ByteArray = jpg.encode(bitmapData); 
			var fr:FileReference = new FileReference(); 
			fr.save(jpgByteArray, 'export.jpg');
		}
		public function initScene(num:int):void
		{			
			//对展示厅初始化
			view.uploadPictureList.enabled = true;
			view.angleComp.angle45Btn.enabled = false;
			view.angleComp.angle90Btn.enabled = false;
			view.angleComp.angle135Btn.enabled = false;
			view.uploadPictureList.selectedIndex = -1;	
			PopUpManager.removePopUp(singleFurnitureTitleWindow);
			mediatorMap.removeMediatorByView(singleFurnitureTitleWindow);
			furnitureModel.furniturCount = 0;
			//按场景角度设置场景角度变换按钮的可用性
			for each (var angle:Object in wholeScene.SceneAngleList)
			{
				switch (angle.Jdid)
				{
					case 30://正45°
						view.angleComp.angle45Btn.enabled = true;
						break;
					case 31://负45°
						view.angleComp.angle135Btn.enabled = true;
						break;
					case 35://90°
						view.angleComp.angle90Btn.enabled = true;
						break;
					default:
						break;						
				}
			}
			
			//设置被选中角度按钮状态
			switch (selectedScene.Jdid)
			{
				case 30: view.angleComp.angle45Btn.selected = true;
					break;
				case 31: view.angleComp.angle135Btn.selected = true;
					break;
				case 35: view.angleComp.angle90Btn.selected = true;
					break;
				default: break;
			}
			
			furniturArray.removeAll();
			for (var i:int = 0; i < selectedScene.SceneInfoList.length; i++)
			{
				furniturArray.addItem(selectedScene.SceneInfoList[i]);
			}
			
			view.hideFurnitureList.visible = true;
			view.hideFurnitureList.dataProvider = furniturArray;
			//BindingUtils.bindProperty(view.hideFurnitureList, "dataProvider", this, "furniturArray");
			addViewListener(HideFurnitureEvent.HIDE_A_FURNITURE, hide);
			addContextListener(HideFurnitureEvent.HIDE_A_FURNITURE, hide);
			eventMap.mapListener(view.angleComp.angle45Btn, MouseEvent.CLICK, changeAngle45Handler);
			eventMap.mapListener(view.angleComp.angle90Btn, MouseEvent.CLICK, changeAngle90Handler);
			eventMap.mapListener(view.angleComp.angle135Btn, MouseEvent.CLICK, changeAngle135Handler);
		}
		
		public function changeAngle45Handler(event:MouseEvent):void
		{
			selectedAngle = 30;
			wholeScene = view.briefMessageList.selectedItem;
			changeAngle();
		}
		
		private function screenShotHandler(event:MouseEvent):void
		{
			var boundingBox:Rectangle = view.borderContainer2.getRect(view.borderContainer2);
			var matrix:Matrix = new Matrix();
			var bitmapData:BitmapData = new BitmapData(boundingBox.width, boundingBox.height);
			bitmapData.draw(view.borderContainer2, matrix); 
			var jpg:JPEGEncoder = new JPEGEncoder(); 
			var jpgByteArray:ByteArray = jpg.encode(bitmapData); 
			var fr:FileReference = new FileReference(); 
			fr.save(jpgByteArray, 'export.jpg');
		}
		
		private function itemMouseDownHandler(event:ListItemMouseEvent):void
		{
			furnitureModel.furniture = new Furniture(event.target.data, event.content);
			DragDropManager.startDragFurniture(view.uploadPictureList.dataGroup, event.myEvent, new Furniture(event.target.data, event.content));
		}
		
		public function showRoomHandler(event:ShowRoomEvent):void
		{		
			wholeScene = event.data;
			selectedScene = event.data.SceneAngleList[0];
			initScene(0);
			var showroom:Showroom = new Showroom(view.roomDisplay, event.data, 0);
		}
		
		
		
		
		
		public function changeAngle90Handler(event:MouseEvent):void
		{
			selectedAngle = 35;
			wholeScene = view.briefMessageList.selectedItem;
			changeAngle();
		}
		
		public function changeAngle135Handler(event:MouseEvent):void
		{
			selectedAngle = 31;
			wholeScene = view.briefMessageList.selectedItem;
			changeAngle();
		}
		
		public function changeAngle():void
		{
			eventMap.unmapListener(view.angleComp.angle45Btn, MouseEvent.CLICK, changeAngle45Handler);
			eventMap.unmapListener(view.angleComp.angle90Btn, MouseEvent.CLICK, changeAngle90Handler);
			eventMap.unmapListener(view.angleComp.angle135Btn, MouseEvent.CLICK, changeAngle135Handler);
			
			for each (var subScene:Object in wholeScene.SceneAngleList)
			{
				if (subScene.Jdid == selectedAngle)
				{
					selectedScene = subScene;
					break;
				}
			}
			
			var showRoom2:Showroom = new Showroom(view.roomDisplay, wholeScene, selectedAngle);
			initScene(selectedAngle);
		}
		
		public function hide(event:HideFurnitureEvent):void
		{	
			event.furniture.sceneInfoBitmap.visible = !event.furniture.sceneInfoBitmap.visible;
		}				
	}
}